﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Diagnostics;

namespace WebTempGen
{
    class ColorSchemeExtractor
    {
        private string htmlColorRegex;

        public ColorSchemeExtractor()
        {
            // Colors in HTML are specified either by name 'red, blue, green' etc
            // or by using RGB values whhich are preceded by # symbol, for example #00ff00
            // They can optionally be surrounded by single or double quotes
            this.htmlColorRegex = "[\"']?([#A-Za-z0-9]+)[\"']?";
        }
        public ColorScheme Extract(WebSiteRetriever.WSRPage page)
        {
            string content = (string)page.Content[UserAgent.Firefox];

            ColorScheme cs = new ColorScheme();
            cs.Background = ExtractBackgroundColor2(content);
            cs.Fonts[0] = ExtractFontColor(content);
            cs.Links = ExtractLinksColor(content);

            int i = 0;
            while (linksNotFound(cs) && i < page.AdditionalPages.Count)
            {
                // if links color was not found, search in the style sheets
                cs.Links = ExtractLinksColor(page.AdditionalPages[i]);
                i++;
            }

            return cs;
        }

        private bool linksNotFound(ColorScheme cs)
        {
            // cs.Links.Count > 0
            return (cs.Links[ColorScheme.LinkType.Link] == Color.Empty
                 || cs.Links[ColorScheme.LinkType.Active] == Color.Empty
                 || cs.Links[ColorScheme.LinkType.Visited] == Color.Empty
                 || cs.Links[ColorScheme.LinkType.Hover] == Color.Empty);
        }

        // Note: doesn't extract from CSS code.
        public string ExtractBackgroundColor(string content)
        {
            // Extract the color
            Match m = Regex.Match(content,
                                 "bgcolor=" + htmlColorRegex, 
                                 RegexOptions.IgnoreCase | RegexOptions.Multiline);
            if (m.Length == 0)
            {
                // Our first regex didn't match for bgcolor attribute. Try the css properties
                m = Regex.Match(content,
                                @"background-color\s*:\s*" + this.htmlColorRegex,
                                RegexOptions.IgnoreCase | RegexOptions.Multiline);
            }

            return m.Groups[1].Value;
        }

        // Returns Color object
        public Color ExtractBackgroundColor2(string content)
        {
            return ColorTranslator.FromHtml(this.ExtractBackgroundColor(content));
        }

        public Color ExtractFontColor(string content)
        {
            Match m = Regex.Match(content,
                                  @"txt=" + htmlColorRegex,
                                  RegexOptions.IgnoreCase | RegexOptions.Multiline);

            if (m.Length == 0)
            {
                // Extract from CSS
                m = Regex.Match(content,
                                @"\s*color\s*:\s*" + this.htmlColorRegex,
                                RegexOptions.IgnoreCase | RegexOptions.Multiline);
            }
            return ColorTranslator.FromHtml(m.Groups[1].Value);
        }

        public Dictionary<ColorScheme.LinkType, Color> ExtractLinksColor(string content)
        {
            // Extract from CSS
            Dictionary<ColorScheme.LinkType, Color> ret = new Dictionary<ColorScheme.LinkType, Color>();

            Match match_link = Regex.Match(content, 
                                  @"\s*A\s*:\s*link\s*{\s*color\s*:\s*" + this.htmlColorRegex + @"\s*;?\s*}",
                                  RegexOptions.IgnoreCase | RegexOptions.Multiline);
            Match match_hover = Regex.Match(content,
                                  @"\s*A\s*:\s*hover\s*{\s*color\s*:\s*" + this.htmlColorRegex + @"\s*;?\s*}",
                                  RegexOptions.IgnoreCase | RegexOptions.Multiline);

            Match match_visited = Regex.Match(content,
                                  @"\s*A\s*:\s*visited\s*{\s*color\s*:\s*" + this.htmlColorRegex + @"\s*;?\s*}",
                                  RegexOptions.IgnoreCase | RegexOptions.Multiline);

            Match match_active = Regex.Match(content,
                                  @"\s*A\s*:\s*active\s*{\s*color\s*:\s*" + this.htmlColorRegex + @"\s*;?\s*}",
                                  RegexOptions.IgnoreCase | RegexOptions.Multiline);

            ret[ColorScheme.LinkType.Link] = ColorTranslator.FromHtml(match_link.Groups[1].Value);
            ret[ColorScheme.LinkType.Hover] = ColorTranslator.FromHtml(match_hover.Groups[1].Value);
            ret[ColorScheme.LinkType.Visited] = ColorTranslator.FromHtml(match_visited.Groups[1].Value);
            ret[ColorScheme.LinkType.Active] = ColorTranslator.FromHtml(match_active.Groups[1].Value);
            return ret;
        }
    }
}
